package cn.com.shopec.erp.base.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;

import cn.com.shopec.erp.common.constant.SZSCMConstant;
import cn.com.shopec.erp.common.page.PageFinder;
import cn.com.shopec.erp.common.page.PageQuery;
import cn.com.shopec.erp.common.utils.JsonHelper2;
import cn.com.shopec.erp.common.utils.StringUtil;
import cn.com.shopec.erp.financing.entity.WhareHouse;
import cn.com.shopec.erp.financing.service.IWhareHouseServer;
import cn.com.shopec.erp.product.service.IProductStockService;

@Controller
@RequestMapping("base/wharehouse")
public class WhaseMaintenController {

	/** 日志. */
	private Logger log = Logger.getLogger(WhaseMaintenController.class);
	
	@Resource private IWhareHouseServer iWhareHouseServer;
	
	
	@Resource private IProductStockService iProductStockService;
	 /**
     * 跳转到仓库信息列表
     * 
     * @return
     */
    @RequestMapping("toWhareHouseList")
    public String toWhareHouseList(ModelMap map) throws Exception {
        return "/base/warehouse_list";
    }
    /**
     * 分布查询仓库信息
     * @param pageQuery
     * @param map
     * @return
     */
	@RequestMapping("ajaxWhareHouseList")
	@ResponseBody
	public String ajaxCollectVoList(PageQuery pageQuery,ModelMap map)  {
		PageFinder<WhareHouse> pageFinder = null;
		try {
			pageFinder = iWhareHouseServer.getPagedCollectSetList(pageQuery);
			JSONObject jsonObj = new JSONObject();
			List<WhareHouse> returnSetVO = pageFinder.getData();
			jsonObj.put("Rows", returnSetVO);
			jsonObj.put("Total", pageFinder.getRowCount());
			return jsonObj.toJSONString();
		} catch (Exception e) {
			log.info("error:"+e.getMessage());
			return "";
		}

	}
	
	
	 /**
     * 修改仓库
     * @param pageQuery
     * @param map
     * @return
	 * @throws Exception 
     */
	@RequestMapping("{whNo}/editWhareHouse")
	public String editWhareHouse(@PathVariable String whNo,ModelMap map) throws Exception  {
		WhareHouse whareHouse= null;
		//修改仓库信息
		if(whNo.length()==32)
		{
			whareHouse= iWhareHouseServer.getWhareHouseByID(whNo);
		}
		else
		{
			whareHouse= iWhareHouseServer.getWhareHouseByNo(whNo);
		}
		map.put("wharehouse", whareHouse);
		return"/base/wharehouse_update";

	}
	
	
	 /**
     * 修改仓库提交
     * @param pageQuery
     * @param map
     * @return
     */
	@RequestMapping(value="{whNo}/editToWhareHouse",method=RequestMethod.POST)
	@ResponseBody
	public String editToWhareHouse(WhareHouse whareHouse,
			HttpServletRequest request,
			ModelMap map)  {
		try {
			whareHouse.setWhNo(request.getParameter("whNo"));
			whareHouse.setWhName(request.getParameter("whName"));
			whareHouse.setWhPhone(request.getParameter("whPhone"));
			whareHouse.setWhUser(request.getParameter("whUser"));
			whareHouse.setWhAdderss(request.getParameter("whAdderss"));
			whareHouse.setWhType(Integer.parseInt(request.getParameter("whType")));
			whareHouse.setDeleteFlag(SZSCMConstant.DELETE_FLAG_NO);	
			//虚库唯一
			if(whareHouse.getWhType().equals(SZSCMConstant.INVENTORYTYPE_2)){
				List<WhareHouse> lists = iWhareHouseServer.getAllWhareTypeTwo();
				if(null!=lists && lists.size()>0){
					if(!lists.get(0).getId().equals(whareHouse.getId())){
						return "虚库唯一,操作失败";
					}
				}
			}
			//修改仓库
			iWhareHouseServer.updateWhareHouse(whareHouse);
		} catch (Exception e) {
			log.info("error:"+e.getMessage());
			return "字符长度过长保存失败";
		}
		return"success";

	}
	
	
	 /**
     * 查询仓库信息
     * @param pageQuery
     * @param map
     * @return
     */
	@RequestMapping(value="ajaxToWhareName",method=RequestMethod.POST)
	@ResponseBody
	public String ajaxToWhareName(@RequestParam String id,
			@RequestParam String whName,
			ModelMap map)  {
		if("undefined".equalsIgnoreCase(id)){
			id=null;
		}
		if(iWhareHouseServer.findWhareHouseByName(id,whName)){
			return "success";
		}else{
			return"error";
		}
	}
	
	 /**
     * 查询仓库信息
     * @param pageQuery
     * @param map
     * @return
     */
	@RequestMapping(value="ajaxToWhareNo",method=RequestMethod.POST)
	@ResponseBody
	public String ajaxToWhareNo(@RequestParam String id,
			@RequestParam String whNo,@RequestParam String whName,
			ModelMap map)  {
		if("undefined".equalsIgnoreCase(id)){
			id=null;
		}
		if(iWhareHouseServer.findWhareHouseByNo(id,whNo)){
			if(iWhareHouseServer.findWhareHouseByName(id,whName)){
				return "success";
			}else{
				return"whNameError";
			}
		}else{
			return"whNoError";
		}
	}
	 /**
     * 删除仓库信息
     * @param pageQuery
     * @param map
     * @return
     */
	@RequestMapping(value="ajaxToDeleteWhareName",method=RequestMethod.POST)
	@ResponseBody
	public String ajaxToDeleteWhareName(
			@RequestParam String whNo,
			ModelMap map)  {
		
		try {
			int count = iProductStockService.getProductCountByWhNo(whNo);
			if(count > 0){
				return "error";
			}else{
				iWhareHouseServer.deleteWhareHouse(whNo);
			}
		} catch (Exception e) {
			log.info("error:"+e.getMessage());
		}
		return "success";
		
	}
	
	@RequestMapping("whareHouseList")
	@ResponseBody
	public String getWhareHouseList(){
		try {
			return JsonHelper2.toJson(iWhareHouseServer.getAllWhareHouse());
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} 
	}
	
	
}
